<%@page language="java" contentType="text/html; charset=utf-8"%> 
<%@page import="freemarker.template.*,java.util.*,com.fasterxml.jackson.databind.*,java.io.*" pageEncoding="utf-8"%>
<%
        //init template engine configure:
        // Initialize the FreeMarker configuration;
        // - Create a configuration instance with the defaults of FreeMarker 2.3.21
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
        // - Templates are stoted in the WEB-INF/templates directory of the Web app.
        cfg.setServletContextForTemplateLoading(
            getServletContext(), "4G/pages/4G");
        // - Give the standard error page on template errors (HTTP 500, usually):
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        // You should set various other settings in a real app.
        // See the "webapp2" example for them.
         

        // Build the data-model
        // Map root = new HashMap();
     //   String pageDataUrl = getServletContext() + "/4G/xxxxx"; //TODO: load from file
        String pageData = request.getParameter("output_cnt");
        pageData = new String(pageData.getBytes("iso-8859-1"),"utf-8");
        System.out.println("page data:" + pageData);
        //out.write(pageData);
        ObjectMapper mapper = new ObjectMapper();
        //解析器支持解析单引号  
        //mapper.configure(Feature.ALLOW_SINGLE_QUOTES,true);
        //解析器支持解析结束符
       // mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true);
        HashMap jsonMap = mapper.readValue(new java.io.StringReader(pageData),HashMap.class); 

        Map root = jsonMap;//loadPageDataFromJson(pageData);
        //root.put("message", "Hello World from freemarker!");
        
        // Get the templat object
        Template t = cfg.getTemplate("index.ftl");
        // Merge the data-model and the template
        try { 
            //StringWriter w = new StringWriter();
            t.process(root, out);
            //String cnt = w.toString();
            //cnt = new String(cnt.getBytes("gbk"),"utf-8");
            //System.out.println("content;\n" +cnt);
            //out.write(cnt);
        } catch (TemplateException e) {
            throw new ServletException(
                    "Error while processing FreeMarker template", e);
        }
/*
        static HashMap loadPageDataFromJson(String jsonData){ 
            ObjectMapper mapper = new ObjectMapper();
            //解析器支持解析单引号
            mapper.configure(Feature.ALLOW_SINGLE_QUOTES,true);
            //解析器支持解析结束符
            mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true);
            HashMap jsonMap = mapper.readValue(new java.io.StringReader(jsonData),HashMap.class); 
            return jsonMap;
        }
*/
%>